/**
 * @filename:ItemServiceImpl 2021-11-09 01:40:37
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2018 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwl.blue.commont.base.BizException;
import com.lwl.blue.commont.base.ErrorCode;
import com.lwl.blue.commont.base.JsonResult;
import com.lwl.blue.commont.base.PageParam;
import com.lwl.blue.dao.ItemDao;
import com.lwl.blue.entity.Item;
import com.lwl.blue.service.ItemService;
import org.hibernate.validator.internal.util.Contracts;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>自定义serviceImpl写在这里</p>
 *
 * <p>说明： 产品服务实现层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemDao, Item> implements ItemService {

    @Resource
    private ItemDao itemDao;

    @Override
    public JsonResult<Page<Item>> pages(PageParam<Item> pageParam) {
        Page<Item> page = new Page<>(pageParam.getPageNum(), pageParam.getPageSize());
        return new JsonResult<Page<Item>>().success(itemDao.pages(page, pageParam.getParam()));
    }

    @Override
    public List<Item> getExportList(Item item) {
        if(!CollectionUtils.isEmpty(item.getIdBatch())){
            List<Integer> idBatch = item.getIdBatch();
            item=new Item();
            item.setIdBatch(idBatch);
        }
        return itemDao.pages(item);
    }

    @Override
    public JsonResult getItemById(Integer id) {
        Item item = itemDao.getItemById(id);
        if (null == item) {
            throw new BizException(ErrorCode.DATA_NOT_EXIST_ERROR.getCode(), ErrorCode.DATA_NOT_EXIST_ERROR.getMsg());
        }
        return new JsonResult().success(item);
    }

    @Override
    public void updatePhoneByFileUpload(String fileName, String filePath) {
        String itemNo=fileNameChangeItemNo(fileName);
        Item item = itemDao.selectOne(new QueryWrapper<Item>().lambda().eq(Item::getItemNo, itemNo));
        if(null==item){
            throw new BizException(ErrorCode.ITEM_NOT_FOUND_ERROR.getCode(),ErrorCode.ITEM_NOT_FOUND_ERROR.getMsg());
        }
        String phone =item.getPhoto();
       if(-1==phone.indexOf(filePath)){
           phone=phone+filePath+";";
           item.setPhoto(phone);
           itemDao.updateById(item);
        }
    }


    /**
     * 文件名转换成code
     * 文件名格式123(1)
     * @param fileName
     * @return
     */
    private String fileNameChangeItemNo(String fileName){
        //文件类型分隔符
         String FILE_FIX=".";
         //订单
         String FILE_F="(";
         String FILE_FT="（";
        if(-1==fileName.lastIndexOf(FILE_FIX)){
            throw new BizException(ErrorCode.FILE_NAME_ERROR.getCode(), "文件格式错误");
        }
        String code=fileName.substring(0,fileName.lastIndexOf(FILE_FIX));
        if(-1!=fileName.indexOf(FILE_F)){
            code=code.substring(0,fileName.indexOf(FILE_F));
        }
        if(-1!=fileName.indexOf(FILE_FT)){
            code=code.substring(0,fileName.indexOf(FILE_FT));
        }
        return code;
    }

}
